﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using ChanExt;
using C.Unite;
using C.Entities;

namespace ArtLand.OA.Project.Project
{
    public partial class Pro_ArticleChange : CommonPageBase
    {
        public string ContentS = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataBaseHelper.BindDrp(Drp_NATURE, EnumData.DataBaseEunm.NATURE);
                BtnColor.OnClientClick = Window1.GetShowReference();
                btnClose.OnClientClick = ActiveWindow.GetHideReference();
                LoadData();
            }
        }

        protected void LoadData()
        {
            if (!string.IsNullOrEmpty(Request["id"]))
            {
                Proj_Article proart = TbsDbSession.Default.From<Proj_Article>().Where(Proj_Article._.ID == int.Parse(Request["id"])).ToFirst();
                if (proart != null)
                {
                    txt_Color.Text = proart.FontColor;
                    if (txt_Color.Text != "")
                    {
                        BtnColor.Text = txt_Color.Text;
                    }
                    txt_EDDATE.Text = proart.EDDATE.Value.ToString();
                    txt_STDATE.Text = proart.STDATE.Value.ToString();
                    txt_Title.Text = proart.BTOPIC;
                    ContentS = proart.COTENT;
                    Drp_NATURE.SelectedValue = proart.NATURE;
                    if (proart.FontIsBold.Value)
                    {
                        btn_Bold.Pressed = true;
                        hid_Bold.Text = "1";
                    }
                    else
                    {
                        hid_Bold.Text = "0";
                    }

                }
            }
        }


        /// <summary>
        /// 取得颜色
        /// </summary>
        /// <returns></returns>
        public string GetColor()
        {
            string[] ColorHex = new string[] { "00", "33", "66", "99", "CC", "FF" };
            string[] SpColorHex = new string[] { "FF0000", "00FF00", "0000FF", "FFFF00", "00FFFF", "FF00FF" };
            StringBuilder sb = new StringBuilder();
            sb.Append("<table id=\"CT\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\" style=\"border-collapse: collapse\" bordercolor=\"000000\"  style=\"cursor:pointer;\">");
            for (int i = 0; i < 2; i++)
            {
                for (int j = 0; j < 6; j++)
                {
                    sb.Append("<tr height=12>");
                    sb.Append("<td width=11 style=\"background-color:#000000\">");

                    if (i == 0)
                    {
                        sb.Append("<td width=11 onclick=\"Selcolor('" + ColorHex[j] + ColorHex[j] + ColorHex[j] + "')\" style=\"background-color:#" + ColorHex[j] + ColorHex[j] + ColorHex[j] + "\">");
                    }
                    else
                    {
                        sb.Append("<td width=11  onclick=\"Selcolor('" + SpColorHex[j] + "')\" style=\"background-color:#" + SpColorHex[j] + "\">");
                    }
                    sb.Append("<td width=11 style=\"background-color:#000000\">");
                    for (int k = 0; k < 3; k++)
                    {
                        for (int l = 0; l < 6; l++)
                        {
                            sb.Append("<td width=11  onclick=\"Selcolor('" + ColorHex[k + i * 3] + ColorHex[l] + ColorHex[j] + "')\" style=\"background-color:#" + ColorHex[k + i * 3] + ColorHex[l] + ColorHex[j] + "\">");
                        }
                    }
                }
            }
            sb.Append("</table>");
            return sb.ToString();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            int returnvalue = 0;
            bool isBold = false;
            if (hid_Bold.Text == "1")
            {
                isBold = true;
            }
            string ueditorContent = Request.Form["UEditor1"].ToString();
            if (ueditorContent == "")
            {
                Alert.Show("请输入公告内容！");
            }
            else
            {
                Proj_Article art = new Proj_Article();
                if (!string.IsNullOrEmpty(Request["id"]))
                {
                    art.ID = int.Parse(Request["id"]);
                    art.Attach();

                    art.NATURE = Drp_NATURE.SelectedValue;
                    art.Adder = CookInfo.EAdminTrueName();
                    art.USERID = CookInfo.EAdminid();
                    art.STDATE = Convert.ToDateTime(txt_STDATE.Text);
                    art.EDDATE = Convert.ToDateTime(txt_EDDATE.Text);
                    art.COTENT = ueditorContent;
                    art.BTOPIC = txt_Title.Text;
                    art.FontColor = txt_Color.Text;
                    art.FontIsBold = isBold;
                    returnvalue = TbsDbSession.Default.Update<Proj_Article>(art);
                }
                else
                {
                    art.DeAttach();
                    art.AddTime = DateTime.Now;
                    art.NATURE = Drp_NATURE.SelectedValue;
                    art.ACYNUB = DFieldSN.SN(EnumData.DataSNEunm.公告编号);
                    art.STDATE = Convert.ToDateTime(txt_STDATE.Text);
                    art.EDDATE = Convert.ToDateTime(txt_EDDATE.Text);
                    art.COTENT = ueditorContent;
                    art.BTOPIC = txt_Title.Text;
                    art.FontColor = txt_Color.Text;
                    art.FontIsBold = isBold;
                    art.Adder = CookInfo.EAdminTrueName();
                    returnvalue = TbsDbSession.Default.Insert<Proj_Article>(art);
                }
                if (returnvalue > 0)
                {
                    Alert.Show("更新成功！", "提示", ActiveWindow.GetHideRefreshReference());
                }
            }
        }

    }
}